package com.dp.struts2.yc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dp.pojo.ImageJson;
import com.dp.pojo.News;
import com.dp.pub.BasePageAction;
import com.dp.pub.SysProperty;
import com.dp.pub.gridtree.GridTreeUtil;
import com.dp.service.jdc.ChannelServiceImpl3;
import com.dp.service.jdc.IndexServiceImpl;
import com.dp.vo.TChannels;
@Controller
@Scope("session")
public class YcindexAction extends BasePageAction implements ServletContextAware {
	
	private static Logger logger = Logger.getLogger(YcindexAction.class);
	
	/**
	 * 一级导航
	 */
	private List<TChannels> navChannels ;
	private List<List<TChannels>> nav2Channels;
	private String navOnChannelId;
    
    /**
     * 左侧菜单栏栏目列表
     */
	private List<TChannels> channelsList ;
	
	private List<List<News>> newTitleLists;
	private String channelId;
	
	 @Autowired
    private ChannelServiceImpl3 channelService;
    @Autowired
	private IndexServiceImpl indexService;
    
	/**在url上第一次访问时进来的action
	 * @return
	 * navChannels
	 */
	public String index(){
	    navChannels = indexService.getChannelsList(SysProperty.getPrep("hdnews_rootch"));
	    nav2Channels = new ArrayList<List<TChannels>>();
	    for (TChannels t : navChannels) {
            List<TChannels> temp = channelService.getLastChannels(t.getChannelId());
            nav2Channels.add(temp);
        }
        channelsList = indexService.getChannelsList(navChannels.get(0).getChannelId());
        navOnChannelId = navChannels.get(0).getChannelId();
        newTitleLists = new ArrayList<List<News>>();
        List<News> newTitleList =null;
        for(int i=0;i<channelsList.size();i++){
            if(channelsList.get(i).getChannelType().equals("image")){
                newTitleList = indexService.getNewTitalList(channelsList.get(i).getChannelId(),6);
            }else {
                newTitleList = indexService.getNewTitalList(channelsList.get(i).getChannelId(),5);
            }
            newTitleLists.add(newTitleList);
        }
        List<News> iamges = indexService.getNewTitalList(channelsList.get(0).getChannelId(),5);
        for (News news : iamges) {
            news.setResourceRid(indexService.getImgRid(news.getCId()));
        }
        request.getSession().setAttribute("channelsList", channelsList);
        request.getSession().setAttribute("imgNewsList", iamges);
        
        
		return "ycindex";
	}
	/**以前点nav.jsp上的链接
	 * 
     * @return
     * navChannels
     */
	@Deprecated
	public String navindex(){
	    
        navChannels = indexService.getChannelsList(SysProperty.getPrep("hdnews_rootch"));
        channelsList = indexService.getChannelsList(channelId);
        List<News> newTitleList =null;
        newTitleLists = new ArrayList<List<News>>();
        for(int i=0;i<channelsList.size();i++){
            newTitleLists.add(newTitleList);
        }
        return "ycindex";
    }
	/**
	 * 首页第一个channel获取图片的json数据
	 */
	@Deprecated
	public void getImage(){
	    response.setContentType("text/xml;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        List<News> iamges = indexService.getNewTitalList(this.channelId,5);
        for (News news : iamges) {
            news.setResourceRid(indexService.getImgRid(news.getCId()));
        }
        List<ImageJson> imageJsons = new ArrayList<ImageJson>();
        ImageJson imageJson ;
        for (News news : iamges) {
            imageJson = new ImageJson();
            imageJson.setUrl("yc/channel/ycchannel!image.do?rid="+news.getResourceRid());
            imageJson.setTime(2000);
            imageJson.setText(news.getContentName());
            imageJson.setLink("news/ycimgnews!imgnews.do?contentId="+news.getContentId()+"&channelId="+news.getChannelId());
            imageJsons.add(imageJson);
            
        }
        String json = GridTreeUtil.getJsonStr(imageJsons);
            try {
                response.getWriter().write(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
	}
	/**
     * 首页第一个channel获取图片的json数据
     */
	@Deprecated
    public void getImage2(){
        response.setContentType("text/xml;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        List<News> iamges = indexService.getNewTitalList(this.channelId,5);
        for (News news : iamges) {
            news.setResourceRid(indexService.getImgRid(news.getCId()));
        }
        List<ImageJson> imageJsons = new ArrayList<ImageJson>();
        ImageJson imageJson ;
        for (News news : iamges) {
            imageJson = new ImageJson();
            imageJson.setUrl("yc/channel/ycchannel!image.do?rid="+news.getResourceRid());
            imageJson.setTime(2000);
            imageJson.setText(news.getContentName());
            imageJson.setLink("news/ycimgnews!imgnews.do?contentId="+news.getContentId()+"&channelId="+news.getChannelId());
            imageJsons.add(imageJson);
            
        }
        String json = GridTreeUtil.getJsonStr(imageJsons);
            try {
                response.getWriter().write(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
    }
	@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub
		
	}
	public List<TChannels> getChannelsList() {
		return channelsList;
	}
	public void setChannelsList(List<TChannels> channelsList) {
		this.channelsList = channelsList;
	}
	public List<TChannels> getNavChannels() {
        return navChannels;
    }
    public void setNavChannels(List<TChannels> navChannels) {
        this.navChannels = navChannels;
    }
    public List<List<News>> getNewTitleLists() {
        return newTitleLists;
    }
    public void setNewTitleLists(List<List<News>> newTitleLists) {
        this.newTitleLists = newTitleLists;
    }
    public String getChannelId() {
        return channelId;
    }
    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }
    public List<List<TChannels>> getNav2Channels() {
        return nav2Channels;
    }
    public void setNav2Channels(List<List<TChannels>> nav2Channels) {
        this.nav2Channels = nav2Channels;
    }
    public String getNavOnChannelId() {
        return navOnChannelId;
    }
    public void setNavOnChannelId(String navOnChannelId) {
        this.navOnChannelId = navOnChannelId;
    }
}
